package com.zhongxi.guoyuan.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.gift.NoviceItemData;
import com.zhongxi.guoyuan.view.play.MoneyTreeActivity;

import java.util.List;

/**
 * Created by Administrator on 2016/12/19.
 */

public class GiftAdapter extends BaseAdapter {

    private Context context;
    private List<NoviceItemData> list;

    public GiftAdapter(Context context, List<NoviceItemData> data) {
        this.context = context;
        this.list = data;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_user_gift, parent, false);
            holder.ivGift = (ImageView) convertView.findViewById(R.id.ivImg);
            holder.tvToShake = (TextView) convertView.findViewById(R.id.tvToShake);
            holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
            holder.tvCondition = (TextView) convertView.findViewById(R.id.tvCondition);
            holder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
            holder.tvProject = (TextView) convertView.findViewById(R.id.tvProject);
            holder.tvLimit = (TextView) convertView.findViewById(R.id.tvLimit);
            holder.tvShare = (TextView) convertView.findViewById(R.id.tvShare);
            holder.tvStatus = (TextView) convertView.findViewById(R.id.tvStatus);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvToShake.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MoneyTreeActivity.class);
                context.startActivity(intent);
                context.startActivity(intent);
            }
        });
        if(null!=list.get(position)){
            NoviceItemData data = list.get(position);
            if(null!=data.getTitle()&&!"".equals(data.getTitle())){
                holder.tvName.setText(data.getTitle());
            }else{
                holder.tvName.setText("");
            }
            if(null!=data.getName()&&!"".equals(data.getName())){
                holder.tvProject.setText(data.getName());
            }else{
                holder.tvProject.setText("");
            }
            if(null!=data.getCurrencynum()&&!"".equals(data.getCurrencynum())){
                if("1".equals(data.getCurrencytype())){
                    holder.tvCondition.setText(data.getCurrencynum()+"美币");
                }else{
                    holder.tvCondition.setText(data.getCurrencynum()+"银币");
                }
            }
            if(null!=data.getStime()&&null!=data.getEtime()&&!"".equals(data.getStime())&&!"".equals(data.getEtime())){
                holder.tvTime.setText("领取时间："+data.getStime().substring(0,10)+"到"+data.getEtime().substring(0,10));
            }else{
                holder.tvTime.setText("领取时间：");
            }
            if(null!=data.getShareKey()&&!"".equals(data.getShareKey())){  //通过这个可以进入详情页
                holder.tvShare.setVisibility(View.VISIBLE);
                holder.tvStatus.setVisibility(View.VISIBLE);
            }else{
                holder.tvShare.setVisibility(View.GONE);
                holder.tvStatus.setVisibility(View.GONE);
            }
            switch (data.getStatus()){
                case "0":
                    holder.ivGift.setImageResource(R.mipmap.iv_get_gift);
                    break;
                case "1":
                    holder.ivGift.setImageResource(R.mipmap.iv_hasget_gift);
                    break;
                case "2":
                    holder.ivGift.setImageResource(R.mipmap.iv_outtime_gift);
                    break;
            }
        }
        return convertView;
    }

    class ViewHolder {
        private TextView tvName;
        private TextView tvCondition;
        private TextView tvTime;
        private TextView tvToShake;
        private ImageView ivGift;
        private TextView tvProject;
        private TextView tvLimit;
        private TextView tvShare;
        private TextView tvStatus;
    }
}
